﻿using System;
using System.Collections.Generic;
using System.Web;
using Framework.Entities;
using Framework.Exceptions;

namespace Framework.Extensions
{
    public static class GeneralResponseExtension
    {
        public static void CaptureExceptionSilently(this EnhancedSimpleResponse response, Exception ex)
        {
            response.Succeeded = false;
            if (null == response.Errors)
            {
                response.Errors = new List<Error>();
            }

            //var serviceException = ex as Web;
            //if (serviceException != null)
            //{
            //    if (serviceException.HasValidationError)
            //    {
            //        HandleAPIValidationException(response, serviceException);
            //        return;
            //    }
            //}

            CollectException(response, ex);
        }
        public static void CaptureException(this EnhancedSimpleResponse response, Exception ex)
        {
            CaptureExceptionSilently(response, ex);

            if (!_throwExceptionCheckOnHttpRequest) return;

            var shouldThrowException = GetHttpRequestParameter("ShouldThrowException", "false");

            if (!string.IsNullOrEmpty(shouldThrowException)
                && shouldThrowException.Equals("true", StringComparison.OrdinalIgnoreCase))
            {
                throw ex;
            }
        }

        private static bool _throwExceptionCheckOnHttpRequest = true;

        public static void DisableThrowExceptionCheckOnHttpRequest()
        {
            _throwExceptionCheckOnHttpRequest = false;
        }

        public static void EnableThrowExceptionCheckOnHttpRequest()
        {
            _throwExceptionCheckOnHttpRequest = true;
        }

        private static string GetHttpRequestParameter(string name, string defaultValue)
        {
            if (null == HttpContext.Current)
            {
                return defaultValue;
            }

            var value = HttpContext.Current.Request.Params[name];

            if (null == value)
            {
                return defaultValue;
            }

            return value;
        }

        private static void CollectException(EnhancedSimpleResponse response, Exception exception)
        {
            if (null == exception) return;

            if (null == response.StackTrace)
            {
                response.StackTrace = string.Empty;
            }

//            if (exception is DataAccessException ||
//                exception is DataCommandException)
//            {
//                response.Errors.Add(new Error("TechnicalIssue",
//                    "Our system is experiencing technical diffculties, please try again later."));

//                LogHelperV2.Instance.Error(exception);

//#if DEBUG
//                response.Errors.Add(new Error
//                {
//                    Code = exception.GetType().Name,
//                    Message = exception.Message
//                });

//                response.StackTrace += Environment.NewLine + exception.StackTrace;
//#endif

//                return;
//            }

            var businessException = exception as BusinessException;
            if (businessException != null)
            {
                response.Errors.Add(new Error
                {
                    Code = businessException.Code + "@" + businessException.GetType().Name,
                    Message = businessException.Message
                });

                response.StackTrace += Environment.NewLine + businessException.StackTrace;

                CollectException(response, exception.InnerException);
                return;
            }

            response.StackTrace += Environment.NewLine + exception.StackTrace;

            response.Errors.Add(
                new Error
                {
                    Code = "ERROR" + "@" + exception.GetType().Name,
                    Message = exception.Message
                }
                );

            CollectException(response, exception.InnerException);
        }
    }
}
